package com.zelma.agent.mq;

import com.rabbitmq.client.Channel;
import com.zelma.agent.util.MessageUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RabbitListener(queues = "1904")
@Component
public class AgentMq {
  @Autowired
  StringRedisTemplate redisTemplate;
  @RabbitHandler
  public void send(Message message, Channel channel, String tel) throws IOException {
    String s = MessageUtil.sendYzm(tel);
    redisTemplate.opsForValue().set("AgentAuthCode",s,60, TimeUnit.SECONDS);
    System.out.println("验证码["+s+"]，您正在登录宏烨经纪人平台，请妥善保管验证码。");
    try {
      channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    } catch (IOException e) {
      channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
      e.printStackTrace();
    }

  }
}
